package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.THDTO;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.JsonParser;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;

/**
 * 每月特惠
 * @author kuanter
 *
 */
public class PreferentialActivity extends BaseActivity_NoBar {
	RelativeLayout preferential_top;
	String preferential = "每月特惠";
	TextView title;
	
	THDTO th;
	
	WebView pref;
	
	private static final String p_url = "http://www.kuanter.com";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preferentia);
		preferential_top = (RelativeLayout) findViewById(R.id.preferential_top);
		preferential_top.findViewById(R.id.kuanter_right).setVisibility(View.GONE);
		title = (TextView) preferential_top.findViewById(R.id.kuanter_middle);
		title.setText(preferential);
		
		pref = (WebView) findViewById(R.id.preferential);
		pref.getSettings().setJavaScriptEnabled(true);
		pref.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				pref.loadUrl(url);
				return super.shouldOverrideUrlLoading(view, url);
			}
			
		});
		
		DataLoader loader = new DataLoader(PreferentialActivity.this);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair cityID = new BasicNameValuePair("cityCode",SharePrefenceUtil.getCityID(PreferentialActivity.this));
		params.add(cityID);
		loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.findPreferentialInfoOfCity, params, th_handler, "", false);
	}
	
	public void exit(View view) {
		finish();
	}
	
	@Override
    public void onBackPressed() {
        if (pref.canGoBack()) {
        	pref.goBack();
        } else {
            super.onBackPressed();
        }
    }
	
	Handler th_handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == HttpTask.RESULT_OK) {
				String json = (String) msg.obj;
				th = JsonParser.getTH(json);
				if(pref != null) {
					if(th.getUrl() != null && !th.getUrl().equals("") && !th.getUrl().equals("null")) {
						pref.loadUrl(th.getUrl());
					} else {
						pref.loadUrl(p_url);
					}
				}
			} else if(msg.what == HttpTask.RESULT_ERROR) {
				showToast("获取数据失败");
			}
			super.handleMessage(msg);
		}
		
	};
	

	
}
